// packagePay/pages/insurance-current-take-effect/insurance-current-take-effect.js
import { inInsurancePersonnel ,inInsurancePersonnelList } from '../../api/insurance'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        insureType:[],
        tabIndex:0,
        dictValue:'',
        tabHide:true,
        pageSize:20,
        pageNum:1,
        insureList:[],
        hasNextPage:false,
        input:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
    },
    onReady(){
        
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.inInsuranceTab()
        this.inInsuranceList()
    },
    onStateSwitch(event){
        this.setData({
            pageNum:1,
            insureList:[],
            input:''
        })
        console.log(event,'event')
        const {index,value} = event.currentTarget.dataset
        this.setData({
            tabIndex:index,
            dictValue:value
        })
        this.inInsuranceList(this.data.dictValue)
    },
    inInsuranceTab(){
        inInsurancePersonnel({
            insureCompanyType: 'insure_type_rrb'
        }).then(res=>{
            console.log(res,'res')
            if(res.list.length === 1){
                this.setData({
                    tabHide:false
                })
                // this.inInsuranceList(this.data.dictValue)
                return
            }
            let temporary = res.list
            temporary.unshift({
                dictLabel:'全部',
                dictValue:'',
                guaranteeCount:res.allTheNumber
            })
            this.setData({
                insureType:temporary
            })
            
        })

    },
    inInsuranceList(dictType='',searchText=''){
        
        inInsurancePersonnelList({
            dictType:dictType,
            searchText:searchText,
            pageSize:this.data.pageSize,
            pageNum:this.data.pageNum
        }).then(res=>{
            if(!res.hasNextPage){
                this.setData({
                    hasNextPage:true
                })
            }
            let temporary =[]
            temporary.push(...this.data.insureList)
            temporary.push(...res.list)
            this.setData({
                // [`insureType[0].guaranteeCount`]:res.totalCount,
                insureList:temporary
            })
        })
    },
    // 搜索 当前在保人员
    searchWorker(event){
        console.log(event,'event')
        const { value } = event.detail
        this.data.insureList=[]
        this.data.input = value
        this.inInsuranceList(this.data.dictValue,value)
    },
    // 滚动到底部
    scrollDown(){
        console.log('到底部啦')
        if(this.data.hasNextPage)return
        this.setData({
            pageNum:this.data.pageNum+=1
        })
        this.inInsuranceList(this.data.dictValue)
    }
})